package com.nz2004.service.Impl;

import com.nz2004.entity.Role;
import com.nz2004.entity.RoleVO;
import com.nz2004.mapper.RoleMapper;
import com.nz2004.mapper.UserRoleMapper;
import com.nz2004.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public RoleVO queryRoleVO(Integer userId) {
        List<Role> roles = roleMapper.queryRoles();
        Integer[] integers = userRoleMapper.queryRolesByUserId(userId);
        RoleVO roleVO = new RoleVO();
        roleVO.setRoles(roles);
        roleVO.setUserRoles(integers);
        return roleVO;
    }

    @Override
    public Boolean cleanAndInsertRoles(Integer userId, Integer[] ids) {
        int rows = 0;
        rows = userRoleMapper.cleanByUserId(userId);
        for (Integer id : ids) {
            rows = userRoleMapper.insertRoleByRoleId(id,userId);
        }
        if(rows>0){
            return true;
        }
        return false;
    }
}
